<?php

namespace App\Common\Classes\Tools;

//获取时间段工具
class StartEndTimeTool
{
    protected $startSecond = ' 00:00:00';
    protected $endSecond = ' 23:59:59';

    public function getStartEndTime(String $timeKeyWord='', int $days=2, int $weeks=2, int $months=2):array{
        switch ($timeKeyWord){
            case 'today':
                return $this->getToday();
            case 'yesterday':
                return $this->getYesterday();
            case 'beforeYesterday':
                return $this->getBeforeYesterday();
            case 'thisWeek':
                return $this->getThisWeek();
            case 'lastWeek':
                return $this->getLastWeek();
            case 'thisMonth':
                return $this->getThisMonth();
            case 'lastMonth':
                return $this->getLastMonth();
            case 'thisYear':
                return $this->getThisYear();
            case 'lastYear':
                return $this->getLastYear();
            case 'recentDays':
                return $this->getRecentDays($days);
            case 'recentWeeks':
                return $this->getRecentWeeks($weeks);
            case 'recentMonths':
                return $this->getRecentMonths($months);
            default:
                return $this->getToday();
        }
    }

    //今天
    public function getToday():array{
        $today=date('Y-m-d');
        $data=$this->getStartEnd($today . $this->startSecond, date('Y-m-d H:i:s'));
        $data['today']=$today;
        return $data;
    }
    //昨天
    public function getYesterday():array{
        $yesterday=date('Y-m-d',strtotime('yesterday'));
        $data=$this->getStartEnd($yesterday . $this->startSecond, $yesterday.$this->endSecond);
        $data['yesterday']=$yesterday;
        return $data;
    }
    //前天
    public function getBeforeYesterday():array{
        $beforeYesterday=date('Y-m-d',strtotime('-2 days'));
        $data=$this->getStartEnd($beforeYesterday . $this->startSecond, $beforeYesterday.$this->endSecond);
        $data['beforeYesterday']=$beforeYesterday;
        return $data;
    }
    //本周
    public function getThisWeek():array{
        $weekDay=date('N');
        $thisMonday=date('Y-m-d',strtotime('-'.($weekDay-1).' days'));
        $today=date('Y-m-d');
        $data=$this->getStartEnd($thisMonday.$this->startSecond, date('Y-m-d H:i:s'));
        $data['thisWeek']=$thisMonday.' '.$today;
        return $data;
    }
    //上周
    public function getLastWeek():array{
        $weekDay=(int)date('N');
        $days=$weekDay+7-1;
        $lastMonday=date('Y-m-d',strtotime('-'.$days.' days'));
        $lastSunday=date('Y-m-d',strtotime('-'.($weekDay-1).' days'));
        $data=$this->getStartEnd($lastMonday.$this->startSecond, $lastSunday.$this->endSecond);
        $data['lastWeek']=$lastMonday.' '.$lastSunday;
        return $data;
    }
    //本月
    public function getThisMonth():array{
        $thisMonth=date('Y-m');
        $data=$this->getStartEnd($thisMonth . '-1' . $this->startSecond, date('Y-m-d H:i:s'));
        $data['thisMonth']=$thisMonth;
        return $data;
    }
    //上月
    public function getLastMonth():array{
        $thisMonth=(int)date('m');
        $year=(int)date('Y');
        if($thisMonth==1){
            $year-=1;
            $lastMonth=$year.'-12';
        }else{
            $lastMonth=$year.'-'.($thisMonth-1);
        }
        $monthDays=date('t',strtotime($lastMonth));
        $data=$this->getStartEnd($lastMonth.'-1'.$this->startSecond, $lastMonth.'-'.$monthDays.$this->endSecond);
        $data['lastMonth']=$lastMonth;
        return $data;
    }
    //本年
    public function getThisYear():array{
        $thisYear=date('Y');
        $data=$this->getStartEnd($thisYear . '-1-1' . $this->startSecond, date('Y-m-d H:i:s'));
        $data['thisYear']=$thisYear;
        return $data;
    }
    //去年
    public function getLastYear():array{
        $lastYear=(int)date('Y')-1;
        $data=$this->getStartEnd($lastYear . '-1-1' . $this->startSecond, $lastYear . '-12-31' . $this->endSecond);
        $data['lastYear']=$lastYear;
        return $data;
    }
    //最近若干天
    public function getRecentDays(int $days=2):array{
        if(!is_int($days)){
            return [];
        }
        if($days<1){
            return [];
        }
        if($days==1){
            return $this->getToday();
        }
        return $this->getStartEnd(date('Y-m-d',strtotime('-'.($days-1).' days')) . $this->startSecond, date('Y-m-d H:i:s'));
    }
    //最近若干周
    public function getRecentWeeks(int $weeks=2):array{
        if(!is_int($weeks)){
            return [];
        }
        if($weeks<1){
            return [];
        }
        if($weeks==1){
            return $this->getThisWeek();
        }
        $weekDay=(int)date('N');
        $days=$weeks*7;
        $mondayBeforeWeeks=date('Y-m-d',strtotime('-'.($days+$weekDay-1).' days'));
        return $this->getStartEnd($mondayBeforeWeeks.$this->startSecond, date('Y-m-d H:i:s'));
    }
    //最近若干月
    public function getRecentMonths(int $months=2):array{
        if(!is_int($months)){
            return [];
        }
        if($months<1){
            return [];
        }
        if($months==1){
            return $this->getThisMonth();
        }
        $firstDayOfThisMonth=date('Y-m-').'1';  //1号作为参照时间，不能使用29，30，31号作为参照
        $start=date('Y-m-d H:i:s',strtotime($firstDayOfThisMonth.' -'.($months-1).' months'));
        return $this->getStartEnd($start, date('Y-m-d H:i:s'));
    }

    //
    public function getStartEnd($start,$end):array{
        return [
            'start'=>$start,
            'end'=>$end,
        ];
    }


}
